package com.summer.report.web.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.summer.common.model.AjaxResult;
import com.summer.report.common.domain.ReportDataSource;
import com.summer.report.common.service.ReportDataSourceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;

@RestController
@RequestMapping("/datasource")
public class ReportDataSourceController {

    @Autowired
    private ReportDataSourceService reportDataSourceService;

    @RequestMapping("/query")
    public IPage<ReportDataSource> query(ReportDataSource reportDataSource,Long current,Long size){
        Page<ReportDataSource> p=new Page<>(current,size);
        IPage<ReportDataSource> page = reportDataSourceService.page(p, new QueryWrapper<>(reportDataSource).orderBy(true, false, "id"));
        return page;
    }

    @RequestMapping("/delete")
    public AjaxResult delete(Long id){
        Assert.notNull(id, "id不能为空");
        reportDataSourceService.removeById(id);
        return new AjaxResult(true);
    }

    @RequestMapping("/create")
    public AjaxResult create(ReportDataSource reportDataSource){
        reportDataSourceService.save(reportDataSource);
        return new AjaxResult(true);
    }

    @RequestMapping("/edit")
    public AjaxResult edit(ReportDataSource reportDataSource){
        Assert.notNull(reportDataSource.getId(), "id不能为空");
        reportDataSourceService.updateById(reportDataSource);
        return new AjaxResult(true);
    }

    @RequestMapping("/testConnect")
    public AjaxResult testConnect(ReportDataSource reportDataSource){
        Map<String, Object> result = reportDataSourceService.testConnect(reportDataSource);
        if (result.get("result").equals(true)){
            return new AjaxResult((Boolean) result.get("result"));
        }else {
            return new AjaxResult((Boolean) result.get("result"),result.get("msg").toString());
        }
    }
}
